package MyGui;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

import Engines.*;
import DistSys.*;
import Element.*;
import Element.Geom.*;

public class ReliabilityOptionsDlg extends MyDlgFrame implements ActionListener{

  private ZoneComboBox comboZone;
  private JRadioButton rbRadial, rbNetwork;
  private JCheckBox checkDisableSag;
  private JCheckBox checkDisableCapFlow, checkDisable2ndOrder, checkUseNetReduction;
  private JButton btnSet;

  public ReliabilityOptionsDlg(MainFrame parent, String title) {
    super(parent, title);
    try {
      jbInit();
    }
    catch(Exception e) {
      JOptionPane.showMessageDialog(this, e );//e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
      btnSet = new JButton("Set");
      btnSet.setFont( GuiConst.fontDefault );

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(2,2,2,2);
      gbc.fill = GridBagConstraints.BOTH;
      gbc.anchor =GridBagConstraints.WEST;
      gbc.gridheight = 1;
      gbc.gridwidth  = 1;

      this.getContentPane().setLayout( gridBagLayout );

      gbc.gridx=0; gbc.gridy=0; gbc.gridheight = 4;
      this.getContentPane().add( createFirstPanel(), gbc );
      gbc.gridx=0; gbc.gridy=4; gbc.gridheight = 2;
      this.getContentPane().add( createSecondPanel(), gbc );
      gbc.gridx=0; gbc.gridy=6; gbc.gridheight = 4;
      this.getContentPane().add( createThirdPanel(), gbc );

      this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);//this is by default
      this.setSize(220,330);
      this.setLocation(200,200);
 //     this.setResizable(false);

      comboZone.addActionListener(this);
      checkDisableSag.addActionListener(this);
      checkDisableCapFlow.addActionListener(this);
      checkDisable2ndOrder.addActionListener(this);
      checkUseNetReduction.addActionListener(this);
  }

  private JPanel createFirstPanel(){
      JPanel p = new JPanel( new GridLayout(3,1,4,4));
      p.setBorder( new TitledBorder("Root Cause Area"));

      comboZone = new ZoneComboBox();
      comboZone.setEditable(true);
      comboZone.setFont( GuiConst.fontDefault );

      rbRadial = new JRadioButton("Radial Analysis");
      rbRadial.setFont( GuiConst.fontDefault );
      rbRadial.setSelected(true);
      rbRadial.setEnabled(false);

      rbNetwork= new JRadioButton("Network Analysis");
      rbNetwork.setFont( GuiConst.fontDefault );
      rbNetwork.setEnabled(false);

      p.add(comboZone);
      p.add(rbRadial);
      p.add(rbNetwork);
      return p;
  }

  private JPanel createSecondPanel(){
      JPanel p = new JPanel( new GridLayout(1,1,4,4));
      p.setBorder( new TitledBorder("Radial Assessment Options"));

      checkDisableSag = new JCheckBox("Disable Sag Calculations");
      checkDisableSag.setFont( GuiConst.fontDefault );
      checkDisableSag.setSelected( EngineProcessor.bDisableSag );

      p.add( checkDisableSag );
      return p;
  }

  private JPanel createThirdPanel(){
      JPanel p = new JPanel( new GridLayout(3,1,4,4));
      p.setBorder( new TitledBorder("Network Assessment Options"));

      checkDisableCapFlow = new JCheckBox("Disable Capacity Flow");
      checkDisableCapFlow.setFont( GuiConst.fontDefault );
      checkDisableCapFlow.setSelected( EngineProcessor.bDisableCapFlow);

      checkDisable2ndOrder = new JCheckBox("Disable 2nd Order Contingency");
      checkDisable2ndOrder.setFont( GuiConst.fontDefault );
      checkDisable2ndOrder.setSelected( EngineProcessor.bDisable2ndOrder);

      checkUseNetReduction = new JCheckBox("Use Network Reduction");
      checkUseNetReduction.setFont( GuiConst.fontDefault );
      checkUseNetReduction.setSelected( EngineProcessor.bUseNetReduction);

      p.add( checkDisableCapFlow );
      p.add( checkDisable2ndOrder );
      p.add( checkUseNetReduction );
      return p;
  }

  public void actionPerformed(ActionEvent evt){
      Object o = evt.getSource();
      if( o == this.comboZone ) {
          try{
              EngineProcessor.dRootCauseZone = comboZone.getSelectedZone();
          }catch (ZoneComboBox.ZoneException e){
              return;
          }
      }
      else if( o == this.checkDisableSag ) {
          EngineProcessor.bDisableSag = checkDisableSag.isSelected(); }
      else if ( o == this.checkDisableCapFlow ){
          EngineProcessor.bDisableCapFlow = checkDisableCapFlow.isSelected();}
      else if ( o == this.checkDisable2ndOrder ) {
          EngineProcessor.bDisable2ndOrder = checkDisable2ndOrder.isSelected();}
      else if ( o== this.checkUseNetReduction ){
          EngineProcessor.bUseNetReduction = checkUseNetReduction.isSelected();}
  }//end of actionPerformed()
}//end of class
